home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zimage.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  15.2 KB  |  522 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1997, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zimage.c,v 1.5 2000/09/19 19:00:54 lpd Exp $ */
  20. /* Image operators */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "estack.h"        /* for image[mask] */
  25. #include "gsstruct.h"
  26. #include "ialloc.h"
  27. #include "igstate.h"
  28. #include "ilevel.h"
  29. #include "store.h"
  30. #include "gscspace.h"
  31. #include "gscssub.h"
  32. #include "gsmatrix.h"
  33. #include "gsimage.h"
  34. #include "gxiparam.h"
  35. #include "stream.h"
  36. #include "ifilter.h"        /* for stream exception handling */
  37. #include "iimage.h"
  38.  
  39. /* Forward references */
  40. private int image_setup(P5(i_ctx_t *i_ctx_p, os_ptr op, gs_image_t * pim,
  41.                const gs_color_space * pcs, int npop));
  42. private int image_proc_process(P1(i_ctx_t *));
  43. private int image_file_continue(P1(i_ctx_t *));
  44. private int image_string_continue(P1(i_ctx_t *));
  45. private int image_cleanup(P1(i_ctx_t *));
  46.  
  47. /* <width> <height> <bits/sample> <matrix> <datasrc> image - */
  48. int
  49. zimage(i_ctx_t *i_ctx_p)
  50. {
  51.     return zimage_opaque_setup(i_ctx_p, osp, false, gs_image_alpha_none,
  52.                    gs_current_DeviceGray_space(igs), 5);
  53. }
  54.  
  55. /* <width> <height> <paint_1s> <matrix> <datasrc> imagemask - */
  56. int
  57. zimagemask(i_ctx_t *i_ctx_p)
  58. {
  59.     os_ptr op = osp;
  60.     gs_image_t image;
  61.  
  62.     check_type(op[-2], t_boolean);
  63.     gs_image_t_init_mask_adjust(&image, op[-2].value.boolval,
  64.                 gs_incachedevice(igs) != CACHE_DEVICE_NONE);
  65.     return image_setup(i_ctx_p, op, &image, NULL, 5);
  66. }
  67.  
  68. /* Setup for [color|alpha]image.  This code isn't used for Level 1, */
  69. /* but it's simpler to include it here. */
  70. int
  71. zimage_multiple(i_ctx_t *i_ctx_p, bool has_alpha)
  72. {
  73.     os_ptr op = osp;
  74.     int spp;            /* samples per pixel */
  75.     int npop = 7;
  76.     os_ptr procp = op - 2;
  77.     const gs_color_space *pcs;
  78.     bool multi = false;
  79.  
  80.     check_int_leu(*op, 4);    /* ncolors */
  81.     check_type(op[-1], t_boolean);    /* multiproc */
  82.     switch ((spp = (int)(op->value.intval))) {
  83.     case 1:
  84.         pcs = gs_current_DeviceGray_space(igs);
  85.         break;
  86.     case 3:
  87.         pcs = gs_current_DeviceRGB_space(igs);
  88.         goto color;
  89.     case 4:
  90.         pcs = gs_current_DeviceCMYK_space(igs);
  91. color:
  92.         if (op[-1].value.boolval) {    /* planar format */
  93.         if (has_alpha)
  94.             ++spp;
  95.         npop += spp - 1;
  96.         procp -= spp - 1;
  97.         multi = true;
  98.         }
  99.         break;
  100.     default:
  101.         return_error(e_rangecheck);
  102.     }
  103.     return zimage_opaque_setup(i_ctx_p, procp, multi,
  104.             (has_alpha ? gs_image_alpha_last : gs_image_alpha_none),
  105.                    pcs, npop);
  106. }
  107.  
  108. /* Common setup for [color|alpha]image. */
  109. /* Fills in format, BitsPerComponent, Alpha. */
  110. int
  111. zimage_opaque_setup(i_ctx_t *i_ctx_p, os_ptr op, bool multi,
  112.             gs_image_alpha_t alpha, const gs_color_space * pcs,
  113.             int npop)
  114. {
  115.     gs_image_t image;
  116.  
  117.     check_int_leu(op[-2], (level2_enabled ? 12 : 8));    /* bits/sample */
  118.     gs_image_t_init(&image, pcs);
  119.     image.BitsPerComponent = (int)op[-2].value.intval;
  120.     image.Alpha = alpha;
  121.     image.format =
  122.     (multi ? gs_image_format_component_planar : gs_image_format_chunky);
  123.     return image_setup(i_ctx_p, op, &image, pcs, npop);
  124. }
  125.  
  126. /* Common setup for [color|alpha]image and imagemask. */
  127. /* Fills in Width, Height, ImageMatrix, ColorSpace. */
  128. private int
  129. image_setup(i_ctx_t *i_ctx_p, os_ptr op, gs_image_t * pim,
  130.         const gs_color_space * pcs, int npop)
  131. {
  132.     int code;
  133.  
  134.     check_type(op[-4], t_integer);    /* width */
  135.     check_type(op[-3], t_integer);    /* height */
  136.     if (op[-4].value.intval < 0 || op[-3].value.intval < 0)
  137.     return_error(e_rangecheck);
  138.     if ((code = read_matrix(op - 1, &pim->ImageMatrix)) < 0)
  139.     return code;
  140.     pim->ColorSpace = pcs;
  141.     pim->Width = (int)op[-4].value.intval;
  142.     pim->Height = (int)op[-3].value.intval;
  143.     return zimage_setup(i_ctx_p, (gs_pixel_image_t *) pim, op,
  144.             pim->ImageMask | pim->CombineWithColor, npop);
  145. }
  146.  
  147. /* Common setup for all Level 1 and 2 images, and ImageType 4 images. */
  148. int
  149. zimage_setup(i_ctx_t *i_ctx_p, const gs_pixel_image_t * pim,
  150.          const ref * sources, bool uses_color, int npop)
  151. {
  152.     gx_image_enum_common_t *pie;
  153.     int code =
  154.     gs_image_begin_typed((const gs_image_common_t *)pim, igs,
  155.                  uses_color, &pie);
  156.  
  157.     if (code < 0)
  158.     return code;
  159.     return zimage_data_setup(i_ctx_p, (const gs_pixel_image_t *)pim, pie,
  160.                  sources, npop);
  161. }
  162.  
  163. /* Common setup for all Level 1 and 2 images, and ImageType 3 and 4 images. */
  164. /*
  165.  * We push the following on the estack.
  166.  *      control mark,
  167.  *    num_sources,
  168.  *      for I = num_sources-1 ... 0:
  169.  *          data source I,
  170.  *          aliasing information:
  171.  *              if source is not file, 1, except that the topmost value
  172.  *          is used for bookkeeping in the procedure case (see below);
  173.  *              if file is referenced by a total of M different sources and
  174.  *                this is the occurrence with the lowest I, M;
  175.  *              otherwise, -J, where J is the lowest I of the same file as
  176.  *                this one;
  177.  *      current plane index,
  178.  *      num_sources,
  179.  *      enumeration structure.
  180.  */
  181. #define NUM_PUSH(nsource) ((nsource) * 2 + 5)
  182. /*
  183.  * We can access these values either from the bottom (esp at control mark - 1,
  184.  * EBOT macros) or the top (esp = enumeration structure, ETOP macros).
  185.  * Note that all macros return pointers.
  186.  */
  187. #define EBOT_NUM_SOURCES(ep) ((ep) + 2)
  188. #define EBOT_SOURCE(ep, i)\
  189.   ((ep) + 3 + (EBOT_NUM_SOURCES(ep)->value.intval - 1 - (i)) * 2)
  190. #define ETOP_SOURCE(ep, i)\
  191.   ((ep) - 4 - (i) * 2)
  192. #define ETOP_PLANE_INDEX(ep) ((ep) - 2)
  193. #define ETOP_NUM_SOURCES(ep) ((ep) - 1)
  194. int
  195. zimage_data_setup(i_ctx_t *i_ctx_p, const gs_pixel_image_t * pim,
  196.           gx_image_enum_common_t * pie, const ref * sources, int npop)
  197. {
  198.     int num_sources = pie->num_planes;
  199.     int inumpush = NUM_PUSH(num_sources);
  200.     int code;
  201.     gs_image_enum *penum;
  202.     int px;
  203.     const ref *pp;
  204.  
  205.     check_estack(inumpush + 2);    /* stuff above, + continuation + proc */
  206.     make_int(EBOT_NUM_SOURCES(esp), num_sources);
  207.     /*
  208.      * Note that the data sources may be procedures, strings, or (Level
  209.      * 2 only) files.  (The Level 1 reference manual says that Level 1
  210.      * requires procedures, but Adobe Level 1 interpreters also accept
  211.      * strings.)  The sources must all be of the same type.
  212.      *
  213.      * The Adobe documentation explicitly says that if two or more of the
  214.      * data sources are the same or inter-dependent files, the result is not
  215.      * defined.  We don't have a problem with the bookkeeping for
  216.      * inter-dependent files, since each one has its own buffer, but we do
  217.      * have to be careful if two or more sources are actually the same file.
  218.      * That is the reason for the aliasing information described above.
  219.      */
  220.     for (px = 0, pp = sources; px < num_sources; px++, pp++) {
  221.     es_ptr ep = EBOT_SOURCE(esp, px);
  222.  
  223.     make_int(ep + 1, 1);    /* default is no aliasing */
  224.     switch (r_type(pp)) {
  225.         case t_file:
  226.         if (!level2_enabled)
  227.             return_error(e_typecheck);
  228.         /* Check for aliasing. */
  229.         {
  230.             int pi;
  231.  
  232.             for (pi = 0; pi < px; ++pi)
  233.             if (sources[pi].value.pfile == pp->value.pfile) {
  234.                 /* Record aliasing */
  235.                 make_int(ep + 1, -pi);
  236.                 EBOT_SOURCE(esp, pi)[1].value.intval++;
  237.                 break;
  238.             }
  239.         }
  240.         /* falls through */
  241.         case t_string:
  242.         if (r_type(pp) != r_type(sources))
  243.             return_error(e_typecheck);
  244.         check_read(*pp);
  245.         break;
  246.         default:
  247.         if (!r_is_proc(sources))
  248.             return_error(e_typecheck);
  249.         check_proc(*pp);
  250.     }
  251.     *ep = *pp;
  252.     }
  253.     if ((penum = gs_image_enum_alloc(imemory, "image_setup")) == 0)
  254.     return_error(e_VMerror);
  255.     code = gs_image_enum_init(penum, pie, (const gs_data_image_t *)pim, igs);
  256.     if (code != 0) {        /* error, or empty image */
  257.     gs_image_cleanup(penum);
  258.     ifree_object(penum, "image_setup");
  259.     if (code >= 0)        /* empty image */
  260.         pop(npop);
  261.     return code;
  262.     }
  263.     push_mark_estack(es_other, image_cleanup);
  264.     esp += inumpush - 1;
  265.     make_int(ETOP_PLANE_INDEX(esp), 0);
  266.     make_int(ETOP_NUM_SOURCES(esp), num_sources);
  267.     make_istruct(esp, 0, penum);
  268.     switch (r_type(sources)) {
  269.     case t_file:
  270.         push_op_estack(image_file_continue);
  271.         break;
  272.     case t_string:
  273.         push_op_estack(image_string_continue);
  274.         break;
  275.     default:        /* procedure */
  276.         push_op_estack(image_proc_process);
  277.         break;
  278.     }
  279.     pop(npop);
  280.     return o_push_estack;
  281. }
  282. /* Pop all the control information off the e-stack. */
  283. private es_ptr
  284. zimage_pop_estack(es_ptr tep)
  285. {
  286.     return tep - NUM_PUSH(ETOP_NUM_SOURCES(tep)->value.intval);
  287. }
  288.  
  289. /*
  290.  * Continuation for procedure data source.  We use the topmost aliasing slot
  291.  * to remember whether we've just called the procedure (1) or whether we're
  292.  * returning from a RemapColor callout (0).
  293.  */
  294. private int
  295. image_proc_continue(i_ctx_t *i_ctx_p)
  296. {
  297.     os_ptr op = osp;
  298.     gs_image_enum *penum = r_ptr(esp, gs_image_enum);
  299.     int px = ETOP_PLANE_INDEX(esp)->value.intval;
  300.     int num_sources = ETOP_NUM_SOURCES(esp)->value.intval;
  301.     uint size, used[gs_image_max_planes];
  302.     gs_const_string plane_data[gs_image_max_planes];
  303.     const byte *wanted;
  304.     int i, code;
  305.  
  306.     if (!r_has_type_attrs(op, t_string, a_read)) {
  307.     check_op(1);
  308.     /* Procedure didn't return a (readable) string.  Quit. */
  309.     esp = zimage_pop_estack(esp);
  310.     image_cleanup(i_ctx_p);
  311.     return_error(!r_has_type(op, t_string) ? e_typecheck : e_invalidaccess);
  312.     }
  313.     size = r_size(op);
  314.     if (size == 0 && ETOP_SOURCE(esp, 0)[1].value.intval == 0)
  315.     code = 1;
  316.     else {
  317.     for (i = 0; i < num_sources; i++)
  318.         plane_data[i].size = 0;
  319.     plane_data[px].data = op->value.bytes;
  320.     plane_data[px].size = size;
  321.     code = gs_image_next_planes(penum, plane_data, used);
  322.     if (code == e_RemapColor) {
  323.         op->value.bytes += used[px]; /* skip used data */
  324.         r_dec_size(op, used[px]);
  325.         ETOP_SOURCE(esp, 0)[1].value.intval = 0; /* RemapColor callout */
  326.         return code;
  327.     }
  328.     }
  329.     if (code) {            /* Stop now. */
  330.     esp = zimage_pop_estack(esp);
  331.     pop(1);
  332.     image_cleanup(i_ctx_p);
  333.     return (code < 0 ? code : o_pop_estack);
  334.     }
  335.     pop(1);
  336.     wanted = gs_image_planes_wanted(penum);
  337.     do {
  338.     if (++px == num_sources)
  339.         px = 0;
  340.     } while (!wanted[px]);
  341.     ETOP_PLANE_INDEX(esp)->value.intval = px;
  342.     return image_proc_process(i_ctx_p);
  343. }
  344. private int
  345. image_proc_process(i_ctx_t *i_ctx_p)
  346. {
  347.     int px = ETOP_PLANE_INDEX(esp)->value.intval;
  348.     gs_image_enum *penum = r_ptr(esp, gs_image_enum);
  349.     const byte *wanted = gs_image_planes_wanted(penum);
  350.     int num_sources = ETOP_NUM_SOURCES(esp)->value.intval;
  351.     const ref *pp;
  352.  
  353.     ETOP_SOURCE(esp, 0)[1].value.intval = 0; /* procedure callout */
  354.     while (!wanted[px]) {
  355.     if (++px == num_sources)
  356.         px = 0;
  357.     ETOP_PLANE_INDEX(esp)->value.intval = px;
  358.     }
  359.     pp = ETOP_SOURCE(esp, px);
  360.     push_op_estack(image_proc_continue);
  361.     *++esp = *pp;
  362.     return o_push_estack;
  363. }
  364.  
  365. /* Continue processing data from an image with file data sources. */
  366. private int
  367. image_file_continue(i_ctx_t *i_ctx_p)
  368. {
  369.     gs_image_enum *penum = r_ptr(esp, gs_image_enum);
  370.     int num_sources = ETOP_NUM_SOURCES(esp)->value.intval;
  371.  
  372.     for (;;) {
  373.     uint min_avail = max_int;
  374.     gs_const_string plane_data[gs_image_max_planes];
  375.     int code;
  376.     int px;
  377.     const ref *pp;
  378.     bool at_eof = false;
  379.  
  380.     /*
  381.      * Do a first pass through the files to ensure that at least
  382.      * one has data available in its buffer.
  383.      */
  384.  
  385.     for (px = 0, pp = ETOP_SOURCE(esp, 0); px < num_sources;
  386.          ++px, pp -= 2
  387.         ) {
  388.         int num_aliases = pp[1].value.intval;
  389.         stream *s = pp->value.pfile;
  390.         int min_left;
  391.         uint avail;
  392.  
  393.         if (num_aliases <= 0)
  394.         num_aliases = ETOP_SOURCE(esp, -num_aliases)[1].value.intval;
  395.         while ((avail = sbufavailable(s)) <=
  396.            (min_left = sbuf_min_left(s)) + num_aliases - 1) {
  397.         int next = s->end_status;
  398.  
  399.         switch (next) {
  400.         case 0:
  401.             s_process_read_buf(s);
  402.             continue;
  403.         case EOFC:
  404.             at_eof = true;
  405.             break;    /* with no data available */
  406.         case INTC:
  407.         case CALLC:
  408.             return
  409.             s_handle_read_exception(i_ctx_p, next, pp,
  410.                         NULL, 0, image_file_continue);
  411.         default:
  412.             /* case ERRC: */
  413.             return_error(e_ioerror);
  414.         }
  415.         break;        /* for EOFC */
  416.         }
  417.         /*
  418.          * Note that in the EOF case, we can get here with no data
  419.          * available.
  420.          */
  421.         if (avail >= min_left)
  422.         avail = (avail - min_left) / num_aliases; /* may be 0 */
  423.         if (avail < min_avail)
  424.         min_avail = avail;
  425.         plane_data[px].data = sbufptr(s);
  426.         plane_data[px].size = avail;
  427.     }
  428.  
  429.     /*
  430.      * Now pass the available buffered data to the image processor.
  431.      * Even if there is no available data, we must call
  432.      * gs_image_next_planes one more time to finish processing any
  433.      * retained data.
  434.      */
  435.  
  436.     {
  437.         int pi;
  438.         uint used[gs_image_max_planes];
  439.  
  440.         code = gs_image_next_planes(penum, plane_data, used);
  441.         /* Now that used has been set, update the streams. */
  442.         for (pi = 0, pp = ETOP_SOURCE(esp, 0); pi < num_sources;
  443.          ++pi, pp -= 2
  444.          )
  445.         sbufskip(pp->value.pfile, used[pi]);
  446.         if (code == e_RemapColor)
  447.         return code;
  448.     }
  449.     if (at_eof)
  450.         code = 1;
  451.     if (code) {
  452.         esp = zimage_pop_estack(esp);
  453.         image_cleanup(i_ctx_p);
  454.         return (code < 0 ? code : o_pop_estack);
  455.     }
  456.     }
  457. }
  458.  
  459. /* Process data from an image with string data sources. */
  460. /* This may still encounter a RemapColor callback. */
  461. private int
  462. image_string_continue(i_ctx_t *i_ctx_p)
  463. {
  464.     gs_image_enum *penum = r_ptr(esp, gs_image_enum);
  465.     int num_sources = ETOP_NUM_SOURCES(esp)->value.intval;
  466.     gs_const_string sources[gs_image_max_planes];
  467.     uint used[gs_image_max_planes];
  468.  
  469.     /* Pass no data initially, to find out how much is retained. */
  470.     memset(sources, 0, sizeof(sources[0]) * num_sources);
  471.     for (;;) {
  472.     int px;
  473.     int code = gs_image_next_planes(penum, sources, used);
  474.  
  475.     if (code == e_RemapColor)
  476.         return code;
  477.     stop_now:
  478.     if (code) {        /* Stop now. */
  479.         esp -= NUM_PUSH(num_sources);
  480.         image_cleanup(i_ctx_p);
  481.         return (code < 0 ? code : o_pop_estack);
  482.     }
  483.     for (px = 0; px < num_sources; ++px)
  484.         if (sources[px].size == 0) {
  485.         const ref *psrc = ETOP_SOURCE(esp, px);
  486.         uint size = r_size(psrc);
  487.  
  488.         if (size == 0) {        /* empty source */
  489.             code = 1;
  490.             goto stop_now;
  491.                 }
  492.         sources[px].data = psrc->value.bytes;
  493.         sources[px].size = size;
  494.         }
  495.     }
  496. }
  497.  
  498. /* Clean up after enumerating an image */
  499. private int
  500. image_cleanup(i_ctx_t *i_ctx_p)
  501. {
  502.     es_ptr ep_top = esp + NUM_PUSH(EBOT_NUM_SOURCES(esp)->value.intval);
  503.     gs_image_enum *penum = r_ptr(ep_top, gs_image_enum);
  504.  
  505.     gs_image_cleanup(penum);
  506.     ifree_object(penum, "image_cleanup");
  507.     return 0;
  508. }
  509.  
  510. /* ------ Initialization procedure ------ */
  511.  
  512. const op_def zimage_op_defs[] =
  513. {
  514.     {"5image", zimage},
  515.     {"5imagemask", zimagemask},
  516.         /* Internal operators */
  517.     {"1%image_proc_continue", image_proc_continue},
  518.     {"0%image_file_continue", image_file_continue},
  519.     {"0%image_string_continue", image_string_continue},
  520.     op_def_end(0)
  521. };
  522.